package jwdt.xadmin.finace.action;

import java.util.List;

import jwdt.common.util.CacheUtil;
import jwdt.xadmin.finace.bean.HaAccount;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.MathUtil;
import com.ezerg.web.Form;
import com.ezerg.web.Module;
import com.ezerg.web.Page;
import com.ezerg.web.annotation.Controller;
import com.ezerg.web.annotation.RequestMapping;

@Controller("/xadmin/finace")
@SuppressWarnings("unchecked")
public class AccountAction extends FinaceAction {

	@RequestMapping("/accountlist")
	public Page doAccountlist(Form f, Module m) throws Exception {
		// 
		int page = f.getInt("page", 1);
		int pageSize = f.getInt("pageSize", sys.getAdminnum());
		
		String sql = " order by px";

		String cdate = f.getString("cdate2");
		if (!"".equals(cdate)) {
			sql = " and cdate = '" + cdate + "'" + sql;
		}
		String name = f.getString("name2");
		if (!"".equals(name)) {
			sql = " and name like '%" + name + "%'" + sql;
		}
		String owner = f.getString("owner2");
		if (!"".equals(owner)) {
			sql = " and owner = '" + owner + "'" + sql;
		}
		f.add("owners", Jdb.get().query(super.sql_selUser, CacheUtil.C0));
		String type = f.getString("type2");
		if (!"".equals(type)) {
			sql = " and type = '" + type + "'" + sql;
		}
		
		int zt = f.getInt("zt2", -1);
		if(zt != -1) {
			sql = " zt=" + f.getInt("zt2", 1) + sql;
		}
		//
		PageUtil pu = new PageUtil(HaAccount.class, sql, page, pageSize);
		List<HaAccount> res = pu.getResult();
		for(HaAccount ha : res) {
			Double cnum = ha.getCnum().doubleValue();
			Double inum = ha.getInum().doubleValue();
			Double onum = ha.getOnum().doubleValue();
			cnum = MathUtil.add(cnum, inum);
			cnum = MathUtil.sub(cnum, onum);
			Double in = (Double)Jdb.get().uniqueResult(super.sql_inAcc, new Object[]{ha.getId()});
			Double out = (Double)Jdb.get().uniqueResult(super.sql_outAcc, new Object[]{ha.getId()});
			//System.out.println(ha.getId() + ";out="+out);
			//
			if(out != null) {
				cnum = MathUtil.sub(cnum, out);
			} else {
				out = 0d;
			}
			out = MathUtil.add(out, onum);
			ha.setOutnum(out);
			if(in != null) {
				cnum = MathUtil.add(cnum, in);
			} else {
				in = 0d;
			}
			in = MathUtil.add(in, inum);
			ha.setInnum(in);
			ha.setYnum(MathUtil.round(cnum, 2));
		}
		f.add("pager", pu.setUrl(m.getFullPath()+".do"));
		return m.defaultPage();
	}

	@RequestMapping("/accountadd")
	public Page doAccountadd(Form f, Module m) throws Exception {
		f.add("owners", Jdb.get().query(super.sql_selUser, CacheUtil.C0));
		return m.findPage(super.url+"accountadd");
	}
	
	@RequestMapping("/accountaddact")
	public Page doAccountaddact(Form f, Module m) throws Exception {
		// 
		HaAccount obj = (HaAccount)f.copy2Class(HaAccount.class);
		String ret = "添加成功";
		try {
			Jdb.get().add(obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "添加出错";
		}
		f.add("ret", ret);
		return doAccountadd(f, m);
	}

	@RequestMapping("/accountedit")
	public Page doAccountedit(Form f, Module m) throws Exception {
		// 
		String id = f.getString("id");
		HaAccount obj = (HaAccount)Jdb.get().get(HaAccount.class, id);
		if(obj == null) {
			f.add("ret", "您要修改的记录不存在：id=" + id);
			return m.findPage("tips");
		}
		f.add("obj", obj);
		f.add("owners", Jdb.get().query(super.sql_selUser, CacheUtil.C0));
		return m.findPage(super.url+"accountedit");
	}

	@RequestMapping("/accounteditact")
	public Page doAccounteditact(Form f, Module m) throws Exception {
		// 
		String id = f.getString("id");
		HaAccount obj = (HaAccount)Jdb.get().get(HaAccount.class, id);
		if(obj == null) {
			f.add("ret", "您要修改的记录不存在：id=" + id);
			return m.findPage("tips");
		}
		obj = (HaAccount)f.copy2Obj(obj);
		String ret = "修改成功";
		try {
			Jdb.get().update(obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "修改出错";
		}
		f.add("ret", ret);
		return m.getRedirectPage(super.url + "accountlist.do");
	}

	@RequestMapping("/accountdel")
	public Page doAccountdel(Form f, Module m) throws Exception {
		// 
		String id = f.getString("id");
		HaAccount obj = (HaAccount)Jdb.get().get(HaAccount.class, id);
		if(obj == null) {
			f.add("ret", "您要删除的记录不存在：id=" + id);
			return m.findPage("tips");
		}
		String ret = "删除成功";
		try {
			Jdb.get().del(obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "删除出错";
		}
		f.add("ret", ret);
		return m.getRedirectPage(super.url + "accountlist.do");
	}

}
